<template>
	<view class="mine-info-container">
		<!-- 头部区域 -->
		<headerSlot :propsColor="'#fff'">
			<template v-slot:header>
				<view class="header-content" @click="closePage()">
					<view class="header-left">
						<view class="back-btn">
							<text class="back-icon">←</text>
						</view>
					</view>
					<view class="header-center">
						<text class="page-title">个人信息</text>
					</view>
					<view class="header-right">
						<view class="header-actions">
							<text class="action-text">编辑</text>
						</view>
					</view>
				</view>
			</template>
		</headerSlot>

		<!-- 主要内容区域 -->
		<view class="main-content">
			<!-- 头像编辑卡片 -->
			<view class="avatar-card">
				<view class="avatar-section">
					<button class="avatar-btn" open-type="chooseAvatar" @chooseavatar="chooseAvatar">
						<view class="avatar-wrapper">
							<image class="avatar-image" :src="`${userInfo.avatar || config.commonUrl + '/avt.png'}`"
								mode="aspectFill"></image>
							<view class="avatar-overlay">
								<text class="edit-icon">📷</text>
							</view>
						</view>
					</button>
					<text class="avatar-tip">点击修改头像</text>
				</view>
			</view>

			<!-- 用户信息卡片 -->
			<view class="info-card">
				<view class="info-list">
					<!-- 用户名 -->
					<view class="info-item">
						<view class="info-label">
							<text class="label-text">用户名</text>
						</view>
						<view class="info-input">
							<input type="nickname" @input="editName" v-model="userInfo.nickname" placeholder="请输入用户名"
								class="input-field" />
						</view>
					</view>

					<!-- 用户名验证提示 -->
					<view class="validation-tip" v-if="containsAdmin">
						<text class="tip-icon">⚠️</text>
						<text class="tip-text">用户名不能包含 'admin'</text>
					</view>

					<!-- 联系电话 -->
					<view class="info-item">
						<view class="info-label">
							<text class="label-text">联系电话</text>
						</view>
						<view class="info-input">
							<input type="text" v-model="userInfo.phone" placeholder="手机号码" class="input-field disabled"
								disabled />
						</view>
					</view>

					<!-- 所在小区 -->
					<view class="info-item">
						<view class="info-label">
							<text class="label-text">所在小区</text>
						</view>
						<view class="info-input">
							<input type="text" v-model="userInfo.communityName" placeholder="小区名称"
								class="input-field disabled" disabled />
						</view>
					</view>
				</view>
			</view>

			<!-- 操作按钮卡片 -->
			<view class="action-card">
				<view class="action-buttons">
					<button class="save-btn" @click="saveInfo()">
						<text class="btn-text">保存修改</text>
					</button>
					<button class="logout-btn" @click="closeLogin()">
						<text class="btn-text">退出登录</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import headerSlot from '@/components/header-slot.vue';
	import config from '@/config/config';
	import storage from "@/utils/storage.js";
	import {
		getUserInfo,
		updateUserInfo,
		uploadAvatarFile
	} from "@/api/members";

	export default {
		components: {
			headerSlot,
		},
		data() {
			return {
				wechatImgUrl: '',
				userInfo: {},
				config,
			}
		},
		onLoad() {
			this.init()
		},
		computed: {
			containsAdmin() {
				return /(\s|^)admin(\s|$)/i.test(this.userInfo.nickname)
			}
		},
		methods: {
			init() {
				//获取用户信息
				getUserInfo().then((user) => {
					this.userInfo = user.data.data
					storage.setUserInfo(user.data.data);
				});
			},
			saveInfo() {
				if (this.containsAdmin) {
					uni.showToast({
						title: "用户名不能包含 'admin'",
						icon: "error",
					});
					return
				}

				if (!this.userInfo.nickname || this.userInfo.nickname.trim() === '') {
					uni.showToast({
						title: "请输入用户名",
						icon: "error",
					});
					return
				}

				//保存用户信息
				updateUserInfo({
					nickname: this.userInfo.nickname,
					avatar: this.userInfo.avatar
				}).then((user) => {
					storage.setUserInfo(user.data.data);
					this.closePage()
				}).catch(err => {
					uni.showToast({
						title: "保存失败",
						icon: "error",
					});
				});
			},
			editName(e) {
				this.userInfo.nickname = e.detail.value
			},
			closePage() {
				uni.navigateBack()
			},
			closeLogin() {
				uni.showModal({
					title: '确认退出',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showToast({
								title: "退出登录",
								icon: "none",
								duration: 1500,
							});

							if (uni.showLoading()) {
								uni.hideLoading();
							}

							storage.setHasLogin(false);
							storage.setAccessToken("");
							storage.setRefreshToken("");
							storage.setUuid("");
							storage.setUserInfo({});

							// 使用redirectTo替代reLaunch，避免断开WebSocket连接
							uni.redirectTo({
								url: '/pages/tabBar/mine'
							})
						}
					}
				})
			},
			chooseAvatar(e) {
				this.wechatImgUrl = e.detail.avatarUrl;
				this.uploadImages(this.wechatImgUrl)
			},
			uploadImages(wechatImgUrl) {
				let self = this
		
				if (wechatImgUrl) {
					uni.showLoading({
						title: '上传中...',
						mask: true
					});

					const token = storage.getAccessToken()
					uni.uploadFile({
						url: `${config.commonApi}/api/upload/wechat-image`,
						name: 'file',
						filePath: wechatImgUrl,
						header: {
							'authorization': token
						},
						success(uploadFileRes) {
							let data = JSON.parse(uploadFileRes.data)
							self.$set(self.userInfo, "avatar", data.data.url)
							uni.hideLoading();
							uni.showToast({
								title: "头像上传成功",
								icon: "success",
							});
						},
						fail(err) {
							uni.hideLoading();
							uni.showToast({
								title: "头像上传失败",
								icon: "error",
							});
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.mine-info-container {
		background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
		min-height: 100vh;
	}

	// 头部样式
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 32rpx;
		width: 100%;

		.header-left {
			.back-btn {
				width: 64rpx;
				height: 64rpx;
				background: rgba(255, 255, 255, 0.9);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.95);
					background: rgba(255, 255, 255, 1);
				}

				.back-icon {
					font-size: 32rpx;
					color: #333;
					font-weight: 600;
				}
			}
		}

		.header-center {
			flex: 1;
			text-align: center;

			.page-title {
				font-size: 36rpx;
				font-weight: 700;
				color: #333;
			}
		}

		.header-right {
			.header-actions {
				.action-text {
					font-size: 24rpx;
					color: #666;
					background: rgba(255, 255, 255, 0.8);
					padding: 8rpx 16rpx;
					border-radius: 20rpx;
				}
			}
		}
	}

	// 主要内容区域
	.main-content {
		padding: 32rpx;
	}

	// 头像编辑卡片
	.avatar-card {
		background: white;
		border-radius: 20rpx;
		margin-bottom: 24rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

		.avatar-section {
			padding: 48rpx 32rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.avatar-btn {
				background: transparent;
				border: none;
				padding: 0;
				margin: 0;
				line-height: 1;
				font-size: inherit;
				margin-bottom: 16rpx;

				&::after {
					border: none;
				}

				.avatar-wrapper {
					position: relative;
					width: 160rpx;
					height: 160rpx;
					border-radius: 50%;
					overflow: hidden;
					box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.15);
					transition: all 0.3s ease;

					&:active {
						transform: scale(0.95);
					}

					.avatar-image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}

					.avatar-overlay {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						background: rgba(0, 0, 0, 0.5);
						display: flex;
						align-items: center;
						justify-content: center;
						opacity: 0;
						transition: all 0.3s ease;

						.edit-icon {
							font-size: 48rpx;
							color: white;
						}
					}

					&:active .avatar-overlay {
						opacity: 1;
					}
				}
			}

			.avatar-tip {
				font-size: 26rpx;
				color: #666;
			}
		}
	}

	// 用户信息卡片
	.info-card {
		background: white;
		border-radius: 20rpx;
		margin-bottom: 24rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

		.info-list {
			padding: 32rpx;

			.info-item {
				display: flex;
				align-items: center;
				padding: 24rpx 0;
				border-bottom: 1px solid #f5f5f5;

				&:last-child {
					border-bottom: none;
				}

				.info-label {
					width: 160rpx;
					flex-shrink: 0;

					.label-text {
						font-size: 28rpx;
						font-weight: 600;
						color: #333;
					}
				}

				.info-input {
					flex: 1;

					.input-field {
						width: 100%;
						height: 60rpx;
						font-size: 28rpx;
						color: #333;
						background: transparent;
						border: none;
						outline: none;

						&.disabled {
							color: #999;
						}

						&::placeholder {
							color: #ccc;
						}
					}
				}
			}

			.validation-tip {
				display: flex;
				align-items: center;
				gap: 8rpx;
				padding: 16rpx 0;

				.tip-icon {
					font-size: 24rpx;
				}

				.tip-text {
					font-size: 24rpx;
					color: #FF4D4F;
				}
			}
		}
	}

	// 操作按钮卡片
	.action-card {
		background: white;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

		.action-buttons {
			.save-btn {
				width: 100%;
				height: 100rpx;
				background: linear-gradient(135deg, #FF6B35 0%, #FE6100 100%);
				border: none;
				border-radius: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				transition: all 0.3s ease;

				&:active {
					transform: translateY(2rpx);
					background: linear-gradient(135deg, #FE6100 0%, #FF6B35 100%);
				}

				.btn-text {
					color: white;
					font-size: 32rpx;
					font-weight: 600;
				}
			}

			.logout-btn {
				width: 100%;
				height: 100rpx;
				background: white;
				border: none;
				border-top: 1px solid #f0f0f0;
				border-radius: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				transition: all 0.3s ease;

				&:active {
					background: #f8f9fa;
				}

				.btn-text {
					color: #FF4D4F;
					font-size: 32rpx;
					font-weight: 600;
				}
			}
		}
	}

	// 兼容旧样式
	.header_slot {
		padding: 0 32rpx;
		width: 100%;
		font-weight: 500;
		font-size: 36rpx;
		color: #2C2622;
	}

	.btnStyle {
		background-color: #ffffff;
	}

	button:after {
		border: 0 !important;
	}

	.icon_image {
		margin-right: 24rpx;
		width: 124rpx;
		height: 124rpx;
		background: #793e25;
		border-radius: 50%;
	}

	.user_info {
		padding: 32rpx;

		.user_info_list {
			padding: 32rpx;
		}
	}

	.pire_box {
		margin: 24rpx 0;
	}

	.save_btn,
	.exit_btn {
		padding: 24rpx;
		color: red;
		text-align: center;
	}

	.exit_btn {
		border-top: 1px solid #eee;
	}
</style>